package wiki.zimo.dao;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import wiki.zimo.constant.Constant;
import wiki.zimo.domain.Page;
import wiki.zimo.domain.Product;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

/**
 * @version v1.0
 * @auther: 子墨
 * @datetime: 2018/11/1 20:46
 * @description: ProductDao数据操作类
 */
public class ProductDao extends BaseDao<Product> {

    /**
     * @auther: 子墨
     * @date: 2018/11/2 9:02
     * @describe: 查找热门商品
     * @param: []
     * @return: java.util.List<wiki.zimo.domain.Product>
     * @version v1.0
     */
    public List<Product> findHotestProducts() throws SQLException {
        String sql = "select * from product where  pflag = ? order by pbrowse desc limit 9";
        return qr.query(sql, new BeanListHandler<>(Product.class), Constant.PRODUCT_ON_SELL);
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/2 8:52
     * @describe: 查找最新商品
     * @param: []
     * @return: java.util.List<wiki.zimo.domain.Product>
     * @version v1.0
     */
    public List<Product> findNewestProducts() throws SQLException {
        String sql = "select * from product where  pflag = ? order by pdate desc limit 9";
        return qr.query(sql, new BeanListHandler<>(Product.class), Constant.PRODUCT_ON_SELL);
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/4 14:27
     * @describe: 按照分类id查找商品
     * @param: [cid]
     * @return: java.util.List<wiki.zimo.domain.Product>
     * @version v1.0
     */
    private List<Product> findAllByCid(String cid) throws SQLException {
        String sql = "select * from product where  pflag = ? and cid = ? order by pdate desc";
        return qr.query(sql, new BeanListHandler<>(Product.class), Constant.PRODUCT_ON_SELL, cid);
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/4 15:06
     * @describe: 按照cid计数
     * @param: [cid]
     * @return: int
     * @version v1.0
     */
    public int countAllbyCid(String cid) throws SQLException {
        return findAllByCid(cid).size();
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/4 15:04
     * @describe: 分页查询商品
     * @param: [page, cid]
     * @return: java.util.List<wiki.zimo.domain.Product>
     * @version v1.0
     */
    public List<Product> findProductsByPage(Page<Product> page, String cid) throws SQLException {
        String sql = "select * from product where cid = ? and pflag = ? order by pdate desc limit ?,?";
        return qr.query(sql, new BeanListHandler<>(Product.class), cid, Constant.PRODUCT_ON_SELL, page.getStartIndex(), page.getPageSize());
    }

    /**
     * @auther: 子墨
     * @date: 2018/11/8 20:32
     * @describe: 模糊查询商品
     * @param: [key, cid, flag]
     * @return: java.util.List<wiki.zimo.domain.Product>
     * @version v1.0
     */
    public List<Product> searchProduct(String key, String cid, boolean flag) throws SQLException {

        String sql = getLikeSql();
        if (flag) {
            sql = getLikeSql() + " and cid = ?";
        }
        int count = countSqlParams(sql, "?");
//		System.out.println("count" + count);
        String[] params = new String[count];
        for (int i = 0; i < params.length - 1; i++) {
            params[i] = key;
        }
        if (flag) {
            params[params.length - 1] = cid;
        } else {
            params[params.length - 1] = key;
        }
        System.out.println(sql);
        System.out.println(Arrays.toString(params));
        System.out.println(count);
        return qr.query(sql, new BeanListHandler<>(Product.class), params);
    }
}
/*public class ProductDao implements Dao<Product,String>{

	// 获取SQL语句的内部类
	class SQL extends SQLUtils<Product> {

	}
	// 获取SQL语句的对象
	SQL s = new SQL();

	@Override
	public Product insert(Product entity) throws SQLException {
//		String sql = "insert into Product values( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		String sql = s.getInsertSql();
		return qr.insert(sql,
				new BeanHandler<>(Product.class),
				entity.getPid(),
				entity.getCid(),
				entity.getPname(),
				entity.getMarket_price(),
				entity.getShop_price(),
				entity.getPimage(),
				entity.getPdate(),
				entity.getPbrowse(),
				entity.getPdesc(),
				entity.getPflag()
		);
	}

	@Override
	public boolean delete(Product entity) throws SQLException {
//		String sql = "delete from Product where pid = ?";
		String sql = s.getDeleteSql();
		int update = qr.update(sql,
				entity.getPid()
		);
		if (update > 0){
			return true;
		}
		return false;
	}

	@Override
	public Product update(Product entity) throws SQLException {
//		String sql = "update Product cid = ?, pname = ?, market_price = ?, shop_price = ?,pimage = ?, pdate = ?, pbrowse = ?, pdesc = ?, pflag = ?,pauditing = ? where pid = ?";
		String sql = s.getUpdateSql();
		int update = qr.update(sql,
				entity.getCid(),
				entity.getPname(),
				entity.getMarket_price(),
				entity.getShop_price(),
				entity.getPimage(),
				entity.getPdate(),
				entity.getPbrowse(),
				entity.getPdesc(),
				entity.getPflag(),
				entity.getPid()
		);
		if (update > 0){
			return findOneById(entity.getPid());
		}
		return null;
	}

	@Override
	public Product findOneById(String id) throws SQLException {
//		String sql = "select * from Product where pid = ? limit 1";
		String sql = s.getFindOneByIdSql();
		return qr.query(sql,new BeanHandler<>(Product.class),id);
	}

	@SuppressWarnings("NoUse")
	@Override
	public Product findOneByParams(String... params) throws SQLException {
		return null;
	}

	@Override
	public List<Product> findAll() throws SQLException {
//		String sql = "select * from Product";
		String sql = s.getFindAllSql();
		return qr.query(sql,new BeanListHandler<>(Product.class));
	}

	@Override
	public int count() throws SQLException {
		return findAll().size();
	}

	*//**
 * @auther: 子墨
 * @date: 2018/11/2 9:02
 * @describe: 查找热门商品
 * @param: []
 * @return: java.util.List<wiki.zimo.domain.Product>
 * @version v1.0
 * @auther: 子墨
 * @date: 2018/11/2 8:52
 * @describe: 查找最新商品
 * @param: []
 * @return: java.util.List<wiki.zimo.domain.Product>
 * @version v1.0
 * @auther: 子墨
 * @date: 2018/11/4 14:27
 * @describe: 按照分类id查找商品
 * @param: [cid]
 * @return: java.util.List<wiki.zimo.domain.Product>
 * @version v1.0
 * @auther: 子墨
 * @date: 2018/11/4 15:06
 * @describe: 按照cid计数
 * @param: [cid]
 * @return: int
 * @version v1.0
 * @auther: 子墨
 * @date: 2018/11/4 15:04
 * @describe: 分页查询商品
 * @param: [page, cid]
 * @return: java.util.List<wiki.zimo.domain.Product>
 * @version v1.0
 * @auther: 子墨
 * @date: 2018/11/8 20:32
 * @describe: 模糊查询商品
 * @param: [key, cid, flag]
 * @return: java.util.List<wiki.zimo.domain.Product>
 * @version v1.0
 * @auther: 子墨
 * @date: 2018/11/2 8:52
 * @describe: 查找最新商品
 * @param: []
 * @return: java.util.List<wiki.zimo.domain.Product>
 * @version v1.0
 * @auther: 子墨
 * @date: 2018/11/4 14:27
 * @describe: 按照分类id查找商品
 * @param: [cid]
 * @return: java.util.List<wiki.zimo.domain.Product>
 * @version v1.0
 * @auther: 子墨
 * @date: 2018/11/4 15:06
 * @describe: 按照cid计数
 * @param: [cid]
 * @return: int
 * @version v1.0
 * @auther: 子墨
 * @date: 2018/11/4 15:04
 * @describe: 分页查询商品
 * @param: [page, cid]
 * @return: java.util.List<wiki.zimo.domain.Product>
 * @version v1.0
 * @auther: 子墨
 * @date: 2018/11/8 20:32
 * @describe: 模糊查询商品
 * @param: [key, cid, flag]
 * @return: java.util.List<wiki.zimo.domain.Product>
 * @version v1.0
 *//*
	public List<Product> findHotestProducts() throws SQLException {
		String sql = "select * from product where  pflag = ? order by pbrowse desc limit 9";
		return qr.query(sql, new BeanListHandler<>(Product.class), Constant.PRODUCT_ON_SELL);
	}

	*//**
 * @auther: 子墨
 * @date: 2018/11/2 8:52
 * @describe: 查找最新商品
 * @param: []
 * @return: java.util.List<wiki.zimo.domain.Product>
 * @version v1.0
 *//*
	public List<Product> findNewestProducts() throws SQLException {
		String sql = "select * from product where  pflag = ? order by pdate desc limit 9";
		return qr.query(sql, new BeanListHandler<>(Product.class), Constant.PRODUCT_ON_SELL);
	}

	*//**
 * @auther: 子墨
 * @date: 2018/11/4 14:27
 * @describe: 按照分类id查找商品
 * @param: [cid]
 * @return: java.util.List<wiki.zimo.domain.Product>
 * @version v1.0
 *//*
	private List<Product> findAllByCid(String cid) throws SQLException {
		String sql = "select * from product where  pflag = ? and cid = ? order by pdate desc";
		return qr.query(sql, new BeanListHandler<>(Product.class), Constant.PRODUCT_ON_SELL,cid);
	}

	*//**
 * @auther: 子墨
 * @date: 2018/11/4 15:06
 * @describe: 按照cid计数
 * @param: [cid]
 * @return: int
 * @version v1.0
 *//*
	public int countAllbyCid(String cid) throws SQLException {
		return findAllByCid(cid).size();
	}

	*//**
 * @auther: 子墨
 * @date: 2018/11/4 15:04
 * @describe: 分页查询商品
 * @param: [page, cid]
 * @return: java.util.List<wiki.zimo.domain.Product>
 * @version v1.0
 *//*
	public List<Product> findProductsByPage(Page<Product> page, String cid) throws SQLException {
		String sql = "select * from product where cid = ? and pflag = ? order by pdate desc limit ?,?";
		return qr.query(sql, new BeanListHandler<>(Product.class), cid, Constant.PRODUCT_ON_SELL, page.getStartIndex(), page.getPageSize());
	}

	*//**
 * @auther: 子墨
 * @date: 2018/11/8 20:32
 * @describe: 模糊查询商品
 * @param: [key, cid, flag]
 * @return: java.util.List<wiki.zimo.domain.Product>
 * @version v1.0
 *//*
	public List<Product> searchProduct(String key, String cid, boolean flag) throws SQLException {

		String sql = s.getLikeSql();
		if (flag){
			sql = s.getLikeSql() + " and cid = ?";
		}
		int count = s.countSqlParams(sql,"?");
//		System.out.println("count" + count);
		String[] params = new String[count];
		for (int i = 0; i < params.length - 1; i++){
			params[i] = key;
		}
		if (flag){
			params[params.length - 1] = cid;
		}else {
			params[params.length - 1] = key;
		}
		System.out.println(sql);
		System.out.println(Arrays.toString(params));
		System.out.println(count);
		return qr.query(sql, new BeanListHandler<>(Product.class),params);
	}
}*/
